{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import OnePy as op\n",
    "from OnePy.custom_module.cleaner_sma import SMA\n",
    "\n",
    "class SmaStrategy(op.StrategyBase):\n",
    "\n",
    "    def __init__(self):\n",
    "\n",
    "        super().__init__()\n",
    "        self.sma1 = SMA(3, 40).calculate\n",
    "        self.sma2 = SMA(5, 40).calculate\n",
    "\n",
    "    def handle_bar(self):\n",
    "        for ticker in self.env.tickers:\n",
    "\n",
    "            if self.sma1(ticker) > self.sma2(ticker):\n",
    "\n",
    "                self.buy(100, ticker, takeprofit=15,\n",
    "                         stoploss=100)\n",
    "            else:\n",
    "                self.sell(100, ticker)\n",
    "\n",
    "\n",
    "TICKER_LIST = ['000001']\n",
    "INITIAL_CASH = 20000\n",
    "FREQUENCY = 'D'\n",
    "START, END = '2012-08-07', '2018-08-07'\n",
    "\n",
    "# 实例化策略，会自动添加到env.strategies中\n",
    "SmaStrategy()\n",
    "\n",
    "# 用MongoDB数据库回测\n",
    "go = op.backtest.stock(TICKER_LIST, FREQUENCY, INITIAL_CASH, START, END)\n",
    "\n",
    "# go.sunny() # 注意，fordward analysis之前不能运行go.sunny(), 不然会报错"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Begin Forward Analysis!\n",
      "+----------------------------------------+ \n",
      "Fromdate: 2012-08-07, Todate: 2013-01-07\n",
      "Timescale: 3 Months.\n",
      "Rollingscale: 2 Months.\n",
      "Total roll 2 times.\n",
      "+----------------------------------------+\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "CRITICAL:OnePy:正在初始化OnePy\n",
      "CRITICAL:OnePy:正在初始化OnePy\n",
      "CRITICAL:OnePy:=============== OnePy初始化成功！ ===============\n",
      "CRITICAL:OnePy:开始寻找OnePiece之旅~~~\n",
      "CRITICAL:OnePy:=============== OnePy初始化成功！ ===============\n",
      "CRITICAL:OnePy:开始寻找OnePiece之旅~~~\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "\n",
      "\n",
      "\n",
      "+--------------------------------+\n",
      "|Fromdate           |  2012-08-07|\n",
      "|Todate             |  2012-11-07|\n",
      "|Initial_Value      |   $20000.00|\n",
      "|Final_Value        |   $19900.69|\n",
      "|Total_Return       |     -0.497%|\n",
      "|Max_Drawdown       |      0.732%|\n",
      "|Max_Duration       |     66 days|\n",
      "|Max_Drawdown_Date  |  2012-10-29|\n",
      "|Sharpe_Ratio       |       -1.85|\n",
      "+--------------------------------+\n",
      "当前是第 1 次, 剩余 0.00 mins\n",
      "+--------------------------------+\n",
      "|Fromdate           |  2012-10-07|\n",
      "|Todate             |  2013-01-07|\n",
      "|Initial_Value      |   $20000.00|\n",
      "|Final_Value        |   $19957.34|\n",
      "|Total_Return       |     -0.213%|\n",
      "|Max_Drawdown       |      0.836%|\n",
      "|Max_Duration       |     54 days|\n",
      "|Max_Drawdown_Date  |  2012-11-30|\n",
      "|Sharpe_Ratio       |       -0.65|\n",
      "+--------------------------------+\n",
      "当前是第 2 次, 剩余 0.00 mins\n",
      "Done!\n"
     ]
    }
   ],
   "source": [
    "go.forward_analysis.run(fromdate=START,\n",
    "                        length_month=3,\n",
    "                        rolling_month=2,\n",
    "                        times=2,\n",
    "                        show_summary=True,\n",
    "                        warning=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
